import { message } from 'antd';
// import { history } from 'umi';
import {
    getPostlist,
    removePost,
    getHostIdlist,
    getPostType,
    addPosturl,
    getPostIdDetail,
    CompanyTree
} from '@/services/organization';

export default {
    namespace: 'post',
    state: {
        postList: [],
        postPagination: {
            current: 1,
            pageSize: 10,
            total: 0
        },
        order: '',
        postIdList: [],
        postDetail: {}, // 职位详情
        postType: [],
        companyTree: [] // 公司树
    },
    effects: {
        // 职位删除
        *postDelete({ payload, success }, { call }) {
            const { id } = payload;
            const result = yield call(removePost, id);
            if (result.code === 10000) {
                message.success(result.msg);
                // eslint-disable-next-line no-unused-expressions
                success && success(result.data);
            }
            if (result.code === 60008) {
                message.success(result.msg);
            }
        },

        // 职位列表展示
        *fetchPostList({ payload }, { call, put }) {
            const result = yield call(getPostlist, payload);
            if (result.code === 10000) {
                const { rows } = result.data;
                if (rows) {
                    rows.forEach(item => {
                        if (item.employeeNumber === -1) {
                            // eslint-disable-next-line no-param-reassign
                            item.employeeNumber = 0;
                        }
                    });
                }
                yield put({
                    type: 'savePost',
                    payload: {
                        postList: result.data.rows ? result.data.rows : [],
                        postPagination: {
                            pageSize: result.data.pageSize,
                            current: result.data.pageNo,
                            total: result.data.total
                        }
                    }
                });
            }
        },

        // 职位成员列表展示
        *fetchPostIdList({ payload }, { call, put }) {
            const { id } = payload;
            const result = yield call(getHostIdlist, id);
            if (result.code === 10000) {
                // const { rows } = result.data;
                if (result.data) {
                    result.data.forEach(item => {
                        if (item.dutyType === -1) {
                            // eslint-disable-next-line no-param-reassign
                            item.dutyType = '';
                        }
                        if (item.dutyType === 1) {
                            // eslint-disable-next-line no-param-reassign
                            item.dutyType = '主岗';
                        }
                        if (item.dutyType === 0) {
                            // eslint-disable-next-line no-param-reassign
                            item.dutyType = '兼任';
                        }
                    });
                }
                console.log('result', result);
                yield put({
                    type: 'savePost',
                    payload: {
                        postIdList: result.data
                    }
                });
            }
        },
        // 添加职位中职位类型详情
        *fetchPostType({ payload }, { call, put }) {
            const result = yield call(getPostType, payload);
            if (result.code === 10000) {
                yield put({
                    type: 'savePost',
                    payload: {
                        postType: result.data
                    }
                });
            }
        },
        // 公司树
        *fetchCompanyTree(_, { call, put }) {
            const result = yield call(CompanyTree);
            if (result.code === 10000) {
                yield put({
                    type: 'savePost',
                    payload: {
                        companyTree: result.data
                    }
                });
            }
        },
        // 添加职位addPost
        *addPost({ payload, success }, { call }) {
            // const { companyId } = payload;
            const result = yield call(addPosturl, payload);
            if (result.code === 10000) {
                message.success(result.msg);
                // eslint-disable-next-line no-unused-expressions
                success && success(result.data);
            }
        },
        // 职位详情
        *editPostIdDetail({ payload }, { call, put }) {
            const { id } = payload;
            const result = yield call(getPostIdDetail, id);
            if (result.code === 10000) {
                yield put({
                    type: 'savePost',
                    payload: {
                        postDetail: result.data
                    }
                });
                // success && success();
                // console.log('result', result);
            }
        }
    },
    reducers: {
        savePost(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        }
    }
};
